#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project: Jmpy
@File   : jmp_plot_gen.py
@Author : Link
@Date   : 2025/5/1 16:00
@Remark : 图形生成器基础组件

"""
from string import Template


class JmpyPlotGen:

    @staticmethod
    def jBase():
        """
        基础组件参考
        :return:
        """
        return Template(
            """
            """
        ).substitute(**locals())

    @staticmethod
    def jBaseDistribution(*args):
        """
        Distribution(
            Stack( 1 ),
            Continuous Distribution(
                Column( :OS ),
                Horizontal Layout( 1 ),
                Vertical( 0 ),
                Process Capability( Use Column Property Specs )
            ),
            ...
            Continuous Distribution(
                Column( :Ron ),
                Horizontal Layout( 1 ),
                Vertical( 0 ),
                Process Capability( Use Column Property Specs )
            )
        );
        :param args:
        :return:
        """
        return Template(
            """
            Distribution(
                Stack( 1 ),
                $plots
            );
            """
        ).substitute(plots=",".join(args))

    @staticmethod
    def jBaseContDis(col: str, c_min: float, c_max: float):
        """
        Continuous Distribution(
            Column( :OS ),
            Horizontal Layout( 1 ),
            Vertical( 0 ),
            Process Capability( Use Column Property Specs )
        )
        不在图里面手动放Limit, 用规格限自动显示
        :param col:
        :param c_min:
        :param c_max:
        :return:
        """
        inc = (c_max - c_min) / 30
        return Template(
            """
        Continuous Distribution(
            Column( :$col ),
            Horizontal Layout( 1 ),
            Vertical( 0 ),
            Process Capability( Use Column Property Specs )
        ),
        SendToReport(
            Dispatch(
                {"$col"},
                "1",
                ScaleBox,
                {Min( $c_min ), Max( $c_max ), Inc( $inc ), Minor Ticks( 4 )}
            )
        )
            """
        ).substitute(**locals())

    @staticmethod
    def jGenScatter(col: str, c_min: float, c_max: float, lo_limit: float, hi_limit: float, ):
        """
        :return:
        """
        inc = (c_max - c_min) / 10
        return Template(
            """
Graph Builder(
    Size( 726, 360 ),
    Show Control Panel( 0 ),
    Fit to Window( "Off" ),
    Variables( X( :PART_ID ), Y( :$col ), Group X( :GROUP ), Color( :FAIL_FLAG ) ),
    Elements( Points( X, Y, Legend( 9 ) ) ),
    SendToReport(
    
        Dispatch(
            {},
            "$col",
            ScaleBox,
            {
            Format( "Best", 12 ), Min( $c_min ), Max( $c_max ), Inc( $inc ), Minor Ticks( 1 ),
            Add Ref Line( $hi_limit, "Solid", "Red", "LO_LIMIT($hi_limit)", 1 ),
            Add Ref Line( $lo_limit, "Solid", "Red", "HI_LIMIT($lo_limit)", 1 )
            }
        )
        
    )
);
            """
        ).substitute(**locals())

    @staticmethod
    def jGenHist(col: str, c_min: float, c_max: float, lo_limit: float, hi_limit: float, ):
        """
        传入列名
        """
        inc = (c_max - c_min) / 30
        return Template(
            """
Graph Builder(
    Size( 726, 360 ),
    Show Control Panel( 0 ),
    Fit to Window( "Off" ),
    Variables( Y( :$col ), Group X( :GROUP ) ),
    Elements( Histogram( Y, Legend( 10 ) ) ),
    SendToReport(
        Dispatch(
            {},
            "$col",
            ScaleBox,
            {
            Format( "Best", 12 ), Min( $c_min ), Max( $c_max ), Inc( $inc ), Minor Ticks( 1 ),
            Add Ref Line( $hi_limit, "Solid", "Red", "LO_LIMIT($hi_limit)", 1 ),
            Add Ref Line( $lo_limit, "Solid", "Red", "HI_LIMIT($lo_limit)", 1 )
            }
        )
    )
);
            """
        ).substitute(**locals())

    @staticmethod
    def jGenLine(col: str, c_min: float, c_max: float, lo_limit: float, hi_limit: float, ):
        inc = (c_max - c_min) / 10
        return Template(
            """
Graph Builder(
    Size( 726, 650 ),
    Show Control Panel( 0 ),
    Fit to Window( "Off" ),
    Variables( X( :PART_ID ), Y( :$col ), Wrap( :GROUP ) ),
    Elements( Line( X, Y, Legend( 5 ) ) ),

    SendToReport(

        Dispatch(
            {},
            "$col",
            ScaleBox,
            {
            Format( "Best", 12 ), Min( $c_min ), Max( $c_max ), Inc( $inc ), Minor Ticks( 1 ),
            Add Ref Line( $hi_limit, "Solid", "Red", "LO_LIMIT($hi_limit)", 1 ),
            Add Ref Line( $lo_limit, "Solid", "Red", "HI_LIMIT($lo_limit)", 1 )
            }
        )

    )
);
            """
        ).substitute(**locals())

    @staticmethod
    def jSmoothLine(col: str, c_min: float, c_max: float, lo_limit: float, hi_limit: float, ):
        inc = (c_max - c_min) / 10
        return Template(
            """
Graph Builder(
    Size( 726, 650 ),
    Show Control Panel( 0 ),
    Fit to Window( "Off" ),
    Variables( X( :PART_ID ), Y( :$col ), Wrap( :GROUP ) ),
    Elements( Smoother( X, Y, Legend( 6 ) ) ),

    SendToReport(

        Dispatch(
            {},
            "$col",
            ScaleBox,
            {
            Format( "Best", 12 ), Min( $c_min ), Max( $c_max ), Inc( $inc ), Minor Ticks( 1 ),
            Add Ref Line( $hi_limit, "Solid", "Red", "LO_LIMIT($hi_limit)", 1 ),
            Add Ref Line( $lo_limit, "Solid", "Red", "HI_LIMIT($lo_limit)", 1 )
            }
        )

    )
);
            """
        ).substitute(**locals())
